package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertisingSet;
import android.bluetooth.le.AdvertisingSetCallback;
import android.bluetooth.le.AdvertisingSetParameters;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.TransportDiscoveryData;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.ParcelUuid;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    BluetoothLeAdvertiser advertiser = null;
    Button button;

    Button buttonon;

    Button buttonoff;

    byte[] ledon = new byte[]{(byte) 0xa2, 0x61, 0x4a, 0x01, (byte) 0xf5, 0x11, 0x11, 0x11, 0x11,
            0x70, (byte) 0x9c, (byte)0xc1,0x01,0x01,0x28,0x1e, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5,
            (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0x55};
    byte[] ledoff = new byte[]{(byte) 0xa2, 0x61, 0x4a, 0x01, (byte) 0xf5, 0x11, 0x11, 0x11, 0x11,
            0x70, (byte) 0x9c,
            (byte)0xc1,//开关,灯光
            0x02,//开关 1开 2关
            0x32,//亮度 0-100
            0x32, //色温 0-100
            (byte) 0xf5, (byte) 0xf5, (byte) 0xf5,
            (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0x55};

    byte[] locpostion = new byte[]{(byte) 0xa2, 0x61, 0x4a, 0x05, (byte) 0xf5, 0x11, 0x11, 0x11, 0x11,
            0x70, (byte) 0x9c, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5,
            (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0xf5, (byte) 0x55};

    AdvertisingSetCallback advertisingSetCallback = new AdvertisingSetCallback() {
        @Override
        public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
            super.onAdvertisingSetStarted(advertisingSet, txPower, status);
            Log.i("------alva------", "onAdvertisingSetStarted");
        }

        @Override
        public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
            super.onAdvertisingSetStopped(advertisingSet);
            Log.i("------alva------", "onAdvertisingSetStopped");
        }

        @Override
        public void onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, int status) {
            super.onAdvertisingEnabled(advertisingSet, enable, status);
            Log.i("------alva------", "onAdvertisingEnabled");
        }

        @Override
        public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
            super.onAdvertisingDataSet(advertisingSet, status);
            Log.i("------alva------", "onAdvertisingDataSet");
        }

        @Override
        public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) {
            super.onScanResponseDataSet(advertisingSet, status);
            Log.i("------alva------", "onScanResponseDataSet");
        }

        @Override
        public void onAdvertisingParametersUpdated(AdvertisingSet advertisingSet, int txPower, int status) {
            super.onAdvertisingParametersUpdated(advertisingSet, txPower, status);
            Log.i("------alva------", "onAdvertisingParametersUpdated");
        }

        @Override
        public void onPeriodicAdvertisingParametersUpdated(AdvertisingSet advertisingSet, int status) {
            super.onPeriodicAdvertisingParametersUpdated(advertisingSet, status);
            Log.i("------alva------", "onPeriodicAdvertisingParametersUpdated");
        }

        @Override
        public void onPeriodicAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
            super.onPeriodicAdvertisingDataSet(advertisingSet, status);
            Log.i("------alva------", "onPeriodicAdvertisingDataSet");
        }

        @Override
        public void onPeriodicAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, int status) {
            super.onPeriodicAdvertisingEnabled(advertisingSet, enable, status);
            Log.i("------alva------", "onPeriodicAdvertisingEnabled");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
        advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAdv(locpostion);
            }
        });
        buttonon = findViewById(R.id.buttonon);
        buttonon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAdv(ledon);
            }
        });

        buttonoff = findViewById(R.id.buttonoff);
        buttonoff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAdv(ledoff);
            }
        });
    }

    void startAdv(byte[] data) {
        AdvertiseSettings settings = new AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER)
                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
                .setConnectable(false)
                .setTimeout(0)
                .build();

        AdvertiseData advertiseData = new AdvertiseData.Builder()
                .setIncludeDeviceName(false)
                .setIncludeTxPowerLevel(false)
                .addManufacturerData(0x54b1, data)// EDDYSTONE_SERVICE_UUID 是Eddystone服务UUID，eddystoneFrameBytes是你的Eddystone帧字节
//                .addTransportDiscoveryData(new TransportDiscoveryData(locpostion))
//                .addServiceData(ParcelUuid.fromString("1c31a261-4a05-f511-1111-11709cf5f5f5"),serviceData)
                .build();

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Log.i("------alva------", "start adv");
        AdvertisingSetParameters mAdvertisingSetParameters = (new AdvertisingSetParameters.Builder()).setLegacyMode(true).setConnectable(false).setScannable(false).setInterval(160).setTxPowerLevel(1).build();
        try{advertiser.stopAdvertisingSet(advertisingSetCallback);}catch(Exception ex){}
        advertiser.startAdvertisingSet(mAdvertisingSetParameters, advertiseData, null, null, null, advertisingSetCallback);
    }

    void stopAdv() {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        advertiser.stopAdvertisingSet(advertisingSetCallback);
    }

}